﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace QLTK.User_Control
{
    public partial class XtraViewstore : XtraUserControl
    {
        public XtraViewstore()
        {
            InitializeComponent();
            show();
        }

        private void show()
        {
            try
            {
                khoTableAdapter.KhoTongTK(qLTKDataSet.Kho);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonThem_Click(object sender, EventArgs e)
        {
            var checkvitri = new QLTKDataSet.KhoDataTable();
            khoTableAdapter.CheckVitri(checkvitri, comboBoxKe.Text, comboBoxTang.Text, comboBoxHop.Text);
            if (comboBoxKe.Text == "" || comboBoxTang.Text == "" || comboBoxHop.Text == "")
            {
                MessageBox.Show("Chọn vị trí", "QLTK");
            }
            else if (checkvitri.Count != 0)
            {
                MessageBox.Show("Vị trí đã tồn tại", "QLTK");
            }
            else
            {
                try
                {
                    khoTableAdapter.Insert(Convert.ToByte(comboBoxKe.Text), Convert.ToByte(comboBoxTang.Text),
                                           Convert.ToByte(comboBoxHop.Text));
                    MessageBox.Show("Thêm thành công", "QLTK");
                    khoTableAdapter.KhoTongTK(qLTKDataSet.Kho);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void gridViewStore_RowStyle(object sender, RowStyleEventArgs e)
        {
            var view = sender as GridView;
            if (e.RowHandle >= 0)
            {
                string status = view.GetRowCellDisplayText(e.RowHandle, view.Columns["TongTK"]);
                if (status == "0")
                {
                    e.Appearance.BackColor = Color.Empty;
                    e.Appearance.BackColor2 = Color.Empty;
                }
                else
                {
                    e.Appearance.BackColor = Color.PeachPuff;
                    e.Appearance.BackColor2 = Color.PeachPuff;
                }
            }
        }

        private void gridViewStore_DoubleClick(object sender, EventArgs e)
        {
            var frmDetailStore = new frmDetailStore(gridViewStore.GetFocusedDataRow());
            frmDetailStore.Show();
        }
    }
}